﻿using UnityEngine;
using System.Collections;

public class GameUtils {

	public static bool InterSectCirCleRect( Vector2 center, float radius, Rect rect )
    {
        Vector3 circleDistance;
        circleDistance.x = System.Math.Abs(center.x - rect.x);
        circleDistance.y = System.Math.Abs(center.y - rect.y);

        if (circleDistance.x > (rect.width / 2 + radius)) { return false; }
        if (circleDistance.y > (rect.height / 2 + radius)) { return false; }

        if (circleDistance.x <= (rect.width / 2)) { return true; }
        if (circleDistance.y <= (rect.height / 2)) { return true; }

        float cornerDistance_sq = ((circleDistance.x - rect.width / 2) * (circleDistance.x - rect.width / 2)) + ( (circleDistance.y - rect.height / 2) * (circleDistance.y - rect.height / 2) );

        return (cornerDistance_sq <= (radius * radius));
    }

    public static double GetAngle(Vector2 a, Vector2 b)
    {
        double dx = b.x - a.x;
        double dy = b.y - a.y;

        double rad = System.Math.Atan2(dy, dx);
        return (rad * 180) / System.Math.PI;
    }

    public static Vector2 PositionToPixel(Vector2 pos, float width, float height)
    {
        return new Vector2(pos.x + (width / 2),
                            pos.y + (height / 2));
    }

    public static Vector2 PixelToPosition(Vector2 pixel, float width, float height)
    {
        return new Vector2(pixel.x - (width / 2),
                           pixel.y - (height / 2));
    }

}

